package com.windgo.controller.wx;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.windgo.bean.LoginUserData;
import com.windgo.bean.LoginUserData1;

import com.windgo.bean.MarketUser;
import com.windgo.bean.UserInfo;

import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.BaseRespVo1;
import com.windgo.bean.wxVo.WxRegistPo;
import com.windgo.bean.wxVo.WxRegistVo;
import com.windgo.bean.wxVo.WxUserResetPo;
import com.windgo.config.realm.MarketToken;
import com.windgo.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Map;

/**
 * @author 张林浩
 * @since 2022/10/14 10:39
 */
@RestController
@RequestMapping("wx/auth")
public class UserAuthController {
    @Autowired
    UserService userService;

    /**
     * 微信端_登录
     *
     * @param map
     * @return
     */
    @PostMapping("login")
    public BaseRespVo<LoginUserData> wxLogin(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        Subject subject = SecurityUtils.getSubject();
        MarketToken authenticationToken = new MarketToken(username, password, "wx");
        try {
            subject.login(authenticationToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return BaseRespVo.failed();
        }

        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        LoginUserData1 loginUserData = new LoginUserData1();
        UserInfo userInfoBean = new UserInfo();
        userInfoBean.setAvatarUrl(primaryPrincipal.getAvatar());
        userInfoBean.setNickName(primaryPrincipal.getUsername());
        loginUserData.setToken((String) subject.getSession().getId());
        loginUserData.setUserInfo(userInfoBean);
        return BaseRespVo.ok(loginUserData);
    }

    /**
     * 微信端_退出登录
     *
     * @return
     */
    @PostMapping("logout")
    public BaseRespVo1 logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo1.ok();
    }


    /**
     * 微信注册_发送验证码
     *
     * @param
     * @return
     */
    @PostMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map<String,String> map) {
        String mobile = map.get("mobile");
        userService.regCaptcha(mobile);
        return BaseRespVo.ok(0, "成功");
    }


    @PostMapping("register")
    public BaseRespVo register(@RequestBody WxRegistPo wxRegistVo) {

        int code = userService.register(wxRegistVo);
        if (code == 703) {
            return BaseRespVo.ok(703, "验证码错误");
        } else if (code == 704) {
            return BaseRespVo.ok(704, "用户名已注册");
        } else if (code == 705) {
            return BaseRespVo.ok(705, "手机号已注册");
        } else if (code == 707) {
            return BaseRespVo.ok(707, "手机号格式不正确");
        } else if (code == 200) {
            WxRegistVo wxRegistVo1 = userService.registerUser(wxRegistVo);
            return BaseRespVo.ok(wxRegistVo1);
        }

        return BaseRespVo.ok(404,"失败");
    }

    @PostMapping("reset")
    public BaseRespVo resetUser(@RequestBody WxUserResetPo wxUserResetPo){
        int code = userService.resetUserInfo(wxUserResetPo);
        if (code == 200){
            return BaseRespVo.ok(0,"成功");
        }else {
            return BaseRespVo.ok(404,"失败");
        }

    }


}
